# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.pro>

_realname=coin
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-docs")
pkgver=4.0.0.r12283.g5297f6c
pkgrel=1
pkgdesc="A high-level 3D graphics toolkit, fully compatible with SGI Open Inventor 2.1 (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
url="https://github.com/coin3d/coin"
license=('spdx:BSD-3-Clause')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-graphviz"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             'git')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-openal")
_commit='5297f6c34c1a2c436bd230914a695f06e152f4d6'
source=("${_realname}"::"git+https://github.com/coin3d/coin.git#commit=${_commit}")
sha256sums=('SKIP')

pkgver() {
  cd "${srcdir}/${_realname}"
  printf "4.0.0.r%s.g%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short=7 HEAD)"
}

build() {
  [[ "${MSYSTEM}" == "MINGW32" ]] && {
    # For more info see https://github.com/coin3d/coin/issues/440
    local _cmake_opts="-DCMAKE_CXX_FLAGS=-fno-ipa-icf"
  }

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DCOIN_BUILD_DOCUMENTATION=ON \
    -DUSE_EXTERNAL_EXPAT=ON \
    -DFONTCONFIG_RUNTIME_LINKING=OFF \
    -DFREETYPE_RUNTIME_LINKING=OFF \
    -DLIBBZIP2_RUNTIME_LINKING=OFF \
    -DOPENAL_RUNTIME_LINKING=OFF \
    -DZLIB_RUNTIME_LINKING=OFF \
    -DGLU_RUNTIME_LINKING=OFF \
    -DCOIN_EXTRA_CFLAGS=-DCOIN_DLL \
    ${_cmake_opts} \
    -S "${_realname}" \
    -B "build-${MSYSTEM}"

  ${MINGW_PREFIX}/bin/cmake.exe --build "build-${MSYSTEM}"
}

check() {
  cd "build-${MSYSTEM}"
  "${MINGW_PREFIX}"/bin/ctest.exe . || warning "Tests failed"
}

package_coin() {
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install "build-${MSYSTEM}"
  install -Dm644 "${srcdir}/${_realname}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"

  # split docs
  mkdir -p dest${MINGW_PREFIX}/share
  mv "${pkgdir}${MINGW_PREFIX}"/share/doc dest${MINGW_PREFIX}/share/doc
}

package_coin-docs() {
  pkgdesc+=" (documentation)"
  depends=()

  mv dest/* "${pkgdir}"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
